<?php
class it_message_model extends MY_Model {
	var $model = 'it_message';
	
	function GetJSON()
	{
		$ls_sql	= "	SELECT
						it_message.id,
						it_message.`from`,
						it_message.`to`,
						it_message.subject,
						it_message.message,
						it_message.read_status,
						it_message.create_user,
						it_message.create_date,
						it_message.modify_user,
						it_message.modify_date
					FROM
						it_message 
					WHERE 
							trim(user_id) ='".USERID."' AND
							type = '".post('type')."'
					ORDER BY ".(post('sort')=='' ? ' create_date ' : post('sort'))." ".(post('order')=='' ? 'desc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
	
	function GetAllUser(){
		$ls_sql	= "	SELECT
						user_id `to`,
						user_name `name`
					FROM
						it_userid
					WHERE status='1'";
		$result = $this->db->query($ls_sql);
		if($result->num_rows() > 0){
			return json_encode($result->result_array());
		}else{
			return NULL;
		}
	}
	
		
	function Add($arr_data){
		$this->db->insert('it_message', $arr_data);
	}
	
	function Update($arr_data, $arr_message){
		$this->db->update('it_message', $arr_data, $arr_message);
	}
	
	function GetSingleView($ai_id)
	{
		$result = $this->db->get_where('it_message', array('id' => $ai_id));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
}
?>